package org.example.postgraduateproject.Dao.imp;

import org.example.postgraduateproject.Dao.UserDao;
import org.example.postgraduateproject.Utils.DbUtils;
import org.example.postgraduateproject.Utils.jdbcUtils;
import org.example.postgraduateproject.entity.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author bin
 * @date 2025-05-11
 */
public class UserDaoImp implements UserDao {
    @Override
    public List<User> selectAllUser() {
        StringBuilder sql = new StringBuilder("select user_id,username,password,email,phone,role,created_at,updated_at,status,avatar_url from users;");
        List<User> users = new ArrayList<>();
        Connection conn = DbUtils.getConnection();
        ResultSet rst = null;
        try {
            PreparedStatement pst = conn.prepareStatement(sql.toString());
            rst = jdbcUtils.executeQuery(conn, sql);
            while (rst.next()) {
                User u = new User();
                User user = new User();
                user.setUserId(rst.getInt("user_id"));
                user.setUserName(rst.getString("username"));
                user.setPassWord(rst.getString("password"));
                user.setEmail(rst.getString("email"));
                user.setPhone(rst.getString("phone"));
                user.setRole(rst.getString("role"));
                user.setCreatedAt(rst.getDate("created_at"));
                user.setUpdatedAt(rst.getDate("updated_at"));
                user.setStatus(rst.getString("status"));
                user.setAvatarUrl(rst.getString("avatar_url"));
                users.add(u);
            }
            return users;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public User loginSelectPhone(String phone) {
        StringBuilder sql = new StringBuilder("select user_id,username,password,email,phone,role,created_at,updated_at,status,avatar_url from users where phone =?");
        Connection conn = DbUtils.getConnection();
        PreparedStatement pst = null;
        ResultSet rst = null;
        try {
            pst = conn.prepareStatement(sql.toString());
            rst = jdbcUtils.executeQuery(conn, sql, phone);
            if (rst.next()) {
                User u = new User();
                User user = new User();
                user.setUserId(rst.getInt("user_id"));
                user.setUserName(rst.getString("username"));
                user.setPassWord(rst.getString("password"));
                user.setEmail(rst.getString("email"));
                user.setPhone(rst.getString("phone"));
                user.setRole(rst.getString("role"));
                user.setCreatedAt(rst.getDate("created_at"));
                user.setUpdatedAt(rst.getDate("updated_at"));
                user.setStatus(rst.getString("status"));
                user.setAvatarUrl(rst.getString("avatar_url"));
                return user;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DbUtils.Close(rst, conn, pst);
        }
        return null;
    }

    @Override
    public User loginSelectEmail(String email) {
        StringBuilder sql = new StringBuilder("select user_id,username,password,email,phone,role,created_at,updated_at,status,avatar_url from users where email = ?");
        Connection conn = DbUtils.getConnection();
        PreparedStatement pst = null;
        ResultSet rst = null;
        try {
            pst = conn.prepareStatement(sql.toString());
            rst = jdbcUtils.executeQuery(conn, sql, email);
            if (rst.next()) {
                User u = new User();
                User user = new User();
                user.setUserId(rst.getInt("user_id"));
                user.setUserName(rst.getString("username"));
                user.setPassWord(rst.getString("password"));
                user.setEmail(rst.getString("email"));
                user.setPhone(rst.getString("phone"));
                user.setRole(rst.getString("role"));
                user.setCreatedAt(rst.getDate("created_at"));
                user.setUpdatedAt(rst.getDate("updated_at"));
                user.setStatus(rst.getString("status"));
                user.setAvatarUrl(rst.getString("avatar_url"));
                return user;

            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return null;
    }

    @Override
    public User loginSelectUserName(String userName) {
        StringBuilder sql = new StringBuilder("select user_id,username,password,email,phone,role,created_at,updated_at,status,avatar_url from users where username = ?");
        Connection conn = DbUtils.getConnection();
        ResultSet rst = jdbcUtils.executeQuery(conn, sql, userName);
        try {
            if (rst.next()) {
                User user = new User();
                user.setUserId(rst.getInt("user_id"));
                user.setUserName(rst.getString("username"));
                user.setPassWord(rst.getString("password"));
                user.setEmail(rst.getString("email"));
                user.setPhone(rst.getString("phone"));
                user.setRole(rst.getString("role"));
                user.setCreatedAt(rst.getDate("created_at"));
                user.setUpdatedAt(rst.getDate("updated_at"));
                user.setStatus(rst.getString("status"));
                user.setAvatarUrl(rst.getString("avatar_url"));
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbUtils.Close(rst, conn, null);
        }
        return null;
    }

    @Override
    public int regUser(User user) {
        StringBuilder sql = new StringBuilder("insert into users (username, password, email, phone) values (?,?,?,?)");
        PreparedStatement pst = null;
        Connection conn = DbUtils.getConnection();
        ResultSet rst = null;
        return jdbcUtils.executeUpdate(conn, sql, user.getUserName(), user.getPassWord(), user.getEmail(), user.getPhone());
    }
}
